/*******************************************************************************
																				
	DESCRIPTION:  	This do file plots the LTU risk over time both using predictions 
					obtained year by year and predictions from a fixed year
					to investigate the compositional aspect of cyclicality of LTU.

*******************************************************************************/

clear all
global id_code 128

*******************************************************************************
* Combine predictions obtained using model trained in each year and using fixed model
*******************************************************************************

* First load the data with predictions made using a fixed model, rename varibales

use "${data}/128_Statistics_Full_1992_2016Individuals_TrainedOn_Full2006.dta", clear

keep ymean_* year0 outcome

rename ymean_p ymean_p_fm // suffix "fm" indicates predictions using a fixed model

* Merge with the data with predictions made using model trained on each year, rename varibales
merge 1:1 year0 outcome using "${data}/128_Statistics_Full_1992_2016.dta", keepusing(ymean*)

rename ymean ymean_yy // suffix "yy" indicates predictions using a model trained on each year

keep if _merge == 3
drop _merge

*******************************************************************************
* Merge with the unemployment rate
*******************************************************************************

preserve
use "${data}/101_Employed_Unemployed.dta", clear
keep year shareUnempSS
rename year year0
tempfile temp
save `temp'
restore

merge m:1 year0 using `temp'
drop if _merge==2
drop _merge

*******************************************************************************
* Generate LTU risk instead of JFR rate
*******************************************************************************

gen LTR_fm = (1 - ymean_p_fm)
gen LTR_yy = (1 - ymean_yy)

* Save at 0 months:
preserve

	keep if outcome == "emplAft6M_0M_In"
	
	replace outcome = "At Start of Spell"
	
	keep outcome share* LTR* year
	rename (outcome year0 shareUnempSS LTR_fm LTR_yy) (duration year unempl_rate pred_LTU_risk_2006 actual_LTU_risk)

	export delimited using "${output}/${id_code}_LTU_Risk_byYear_emplAft6M_0M_In_data.csv"
	
restore


*******************************************************************************
* Plots
*******************************************************************************

* Generate the variable and local needed for indication of recessions
gen barUpper = 0.6

foreach outcome in emplAft6M_0M_In emplAft6M_6M_In emplAft6M_12M_In {
	
	if "`outcome'" == "emplAft6M_0M_In" {
		replace barUpper = 0.6
		local max_lab = 0.6
	}
	else if "`outcome'" != "emplAft6M_0M_In" {
		replace barUpper = 0.8
		local max_lab = 0.8
	}
	
	local barcall barUpper year0 if inrange(year, 1992, 1993)| inrange(year, 2008, 2009) | inrange(year, 2012, 2012), lcolor(gs14%50) fcolor(gs14) base(0)	
* Plot showing the LTU risk over the business cycle
	twoway ///
		(bar `barcall') ///
		(line shareUnempSS year0 if inrange(year0, 1992, 2016) & outcome == "`outcome'", color(gs12) yaxis(2)) ///
		(connected LTR_yy year0 if inrange(year0, 1992, 2016) & outcome == "`outcome'", msymbol(O) color(orange_red)) ///
		(connected LTR_fm year0 if inrange(year0, 1992, 2016) & outcome == "`outcome'", msymbol(T) color(ebblue) lp(solid)), ///
		graphregion(color(white)) ///
		ytitle("Average LTU risk") ytitle("Unemployment Rate (%)", angle(180) axis(2)) ///
		xtitle("") ///
		ylabel(0(0.1)`max_lab', angle(0)) ///
		ylabel(0(2)10, angle(0) axis(2)) ///
		xlabel(1992(4)2016) xscale(titlegap(2)) ///
		///
		/* define the legend */ ///
		legend(rows(2) colfirst symxsize(*0.6) ///
			order(2 "Observed LTU Risk" 3 "Predicted LTU Risk (2006 model)" 1 "Recession" 4 "Unemployment Rate") ///
			size(*0.8)) ///
		name(time_`outcome', replace)
		
	graph export "${output}/${id_code}_LTU_Risk_byYear_`outcome'.pdf", as(pdf) replace	
}

*******************************************************************************
* Run regressions of yearly summary stats on unemployment rate
*******************************************************************************	

* Obtain log of the variables
gen l_shareUnempSS=log(shareUnempSS)
gen l_LTR_fm=log(LTR_fm)
gen l_LTR_yy=log(LTR_yy)
		
* Generate time trend
gen trend=year-1991 

* Run regressions
file open myfile using "${output}/${id_code}_Reg_LTU_risk_on_unemp_rate.tex", replace write

file write myfile "\documentclass{article}" _newline ///
	"\usepackage{booktabs}" _newline ///
	"\usepackage[margin=1in]{geometry}" _newline ///
	"\begin{document}" _newline ///
	"\begin{table}[h] \centering" _newline ///
	"\footnotesize \begin{tabular}{@{\extracolsep{2pt}} l c c c c}" _newline ///
	"\hline \hline \addlinespace[1.5ex]" _newline ///
	///
	"& \multicolumn{2}{c}{Predicted log LTU risk (2006)} & \multicolumn{2}{c}{Observable log LTU risk}  \\ \addlinespace[0.1cm]" _newline ///
	"\cline{2-3} \cline{4-5} \addlinespace[0.1cm]" _newline ///
	"& (1) & (2) & (3) & (4) \\ \addlinespace[0.1cm]" _newline ///

foreach year in 1992 1995 {
	
	foreach type in fm yy {

		* Run the regressions with the full panel:
		regress l_LTR_`type' l_shareUnempSS if year0 >= `year' & outcome == "emplAft6M_0M_In"					
		local `type'_nt_beta_u: di %9.3f _b[l_shareUnempSS]
		local `type'_nt_se_u: di %9.3f _se[l_shareUnempSS]
		local `type'_nt_beta_t
		local `type'_nt_se_t
		local `type'_nt_r2: di %9.3f e(r2)
		local `type'_nt_r2_a: di %9.3f e(r2_a)
		local `type'_nt_obs: di %9.0fc e(N)
		
		regress l_LTR_`type' l_shareUnempSS trend if year0 >= `year' & outcome == "emplAft6M_0M_In"
		local `type'_t_beta_u: di %9.3f _b[l_shareUnempSS]
		local `type'_t_se_u: di %9.3f _se[l_shareUnempSS]
		local `type'_t_beta_t: di %9.3f _b[trend]
		local `type'_t_se_t: di %9.3f _se[trend]
		local `type'_t_r2: di %9.3f e(r2)
		local `type'_t_r2_a: di %9.3f e(r2_a)
		local `type'_t_obs: di %9.0fc e(N)
	}
	
	

	file write myfile "& \multicolumn{4}{c}{\textbf{`year'-2016}} \\ \addlinespace[0.01cm]" _newline ///
		"\cline{2-5} \addlinespace[0.1cm]" _newline ///
		"Log unemployment rate & `fm_nt_beta_u' & `fm_t_beta_u' & `yy_nt_beta_u' & `yy_t_beta_u' \\ \addlinespace[0cm]" _newline ///
		" & (`fm_nt_se_u') & (`fm_t_se_u') & (`yy_nt_se_u') & (`yy_t_se_u') \\ \addlinespace[0.2cm]" _newline ///
		"Time trend & `fm_nt_beta_t' & `fm_t_beta_t' & `yy_nt_beta_t' & `yy_t_beta_t' \\ \addlinespace[0cm] " _newline ///	
		" & `fm_nt_se_t' & (`fm_t_se_t') & `yy_nt_se_t' & (`yy_t_se_t') \\ \addlinespace[0.5cm] " _newline ///	
		"\(R^2\) & `fm_nt_r2' & `fm_t_r2' & `yy_nt_r2' & `yy_t_r2' \\ " _newline ///
		"Adj. \(R^2\) & `fm_nt_r2_a' & `fm_t_r2_a' & `yy_nt_r2_a' & `yy_t_r2_a' \\ " _newline ///
		"Observations & `fm_nt_obs' & `fm_t_obs' & `yy_nt_obs' & `yy_t_obs' \\ \addlinespace[0.2cm]" _newline

	if `year' == 1992 {
		file write myfile "\addlinespace[0.3cm]" _newline
	}
		
	if `year' == 1995 {
		file write myfile "\hline \hline \addlinespace[0.2cm]" _newline
	}
}
	
file write myfile ///
	"\end{tabular}" _newline ///
	"\end{table}" _newline ///
	"\end{document}"
	
	
file close myfile			